package client.gui;

import java.awt.Color;
import sharedspace.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.*;
import sharedspace.User;
import client.gui.GUI_AbstractMainMenu.MainMenuType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import messages.MessageAllQuestions;
import messages.MessageAllQuestionsReply;
import messages.MessageSubject;
import messages.MessageSubjectReply;

import java.io.IOException;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.List;
	/**
	 * This JPanel purpose is to show the all questions in the database
	 * @author Amir S.
	 * @author Jacob C.
	 * @author Maya G.
	 * @author Michael V.
	 *  *
	 */
public class GUI_Manager_ViewQuestions extends GUI_AbstractMainMenu{
	private JLabel lblChooseQuestion = null;
	private JList listQuestions = null;
	private DefaultListModel questionsList = null;
	private List<Question> qList = null;
	private JTextPane txtQuestion = null;
	private JTextPane txtAnswer1 = null;
	private JTextPane txtAnswer2 = null;
	private JTextPane txtAnswer3 = null;
	private JTextPane txtAnswer4 = null;
	private JLabel lblQuestion = null;
	private JLabel lblAnswer1 = null;
	private JLabel lblAnswer2 = null;
	private JLabel lblAnswer3 = null;
	private JLabel lblAnswer4 = null;
	public JButton btnBack = null;
	private JLabel lblAuthor = null;
	private JLabel lblSubject = null;
	private JTextPane textSubject = null;
	private JTextPane textAuthor = null;
	private ArrayList<Subject> subjects = null;
	public MessageAllQuestions msgQuestion;
	public MessageSubject msgSubjects;
	private JScrollPane questionScroller = null;	
	Client_GUI ms=null;


	public GUI_Manager_ViewQuestions(Client_GUI MS) {
		super(MainMenuType.MANAGER,MS);
		this.ms = MS;
		this.initiateManager();
		greetMsg.setText("View Questions");
		mainMenu.setText("Manager");

	}
	
	/**
	 * Building the panel contents 
	 */	
	public void initiateManager()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbtnBack(),null);
		background.add(gettxtQuestion(),null);
		background.add(getlistQuestions(),null);
		background.add(btnBack);
		background.add(txtQuestion);
		background.add(txtAnswer1);
		background.add(txtAnswer2);
		background.add(txtAnswer3);
		background.add(txtAnswer4);
		background.add(lblQuestion);
		background.add(lblAnswer1);
		background.add(lblAnswer2);
		background.add(lblAnswer3);
		background.add(lblAnswer4);
		background.add(lblAuthor);
		background.add(lblSubject);
		background.add(textSubject);		
		background.add(textAuthor);	
	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}

	private JScrollPane getlistQuestions()
	{
		int i;
		if (listQuestions == null)
		{
			questionsList = new DefaultListModel();
			// subjects
			msgSubjects=new MessageSubject();
			
			try {
					//Sending the login message to server
					
					ms.client.sendToServer(msgSubjects);	
	
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
	
			MessageSubjectReply msgsubrep = (MessageSubjectReply) ms.client.GetMessage();	
			subjects = msgsubrep.getSubjectList();
			msgQuestion=new MessageAllQuestions();
			
			try {
					//Sending the login message to server
					
					ms.client.sendToServer(msgQuestion);	
	
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
	
			MessageAllQuestionsReply msgqrep = (MessageAllQuestionsReply) ms.client.GetMessage();	
			qList = msgqrep.getQuestionList();
			
			for(i=0;i<msgqrep.QuestionList.size();i++)
			{
				questionsList.addElement("ID: " + msgqrep.getQuestionList().get(i).getQuestionID() + "   ,Subject: " + subjects.get(Integer.parseInt(msgqrep.getQuestionList().get(i).getQuestionSubject())-1).getSubjectName() + "   ,Author: " + msgqrep.getQuestionList().get(i).getAuthor().getUserFirstName() + msgqrep.getQuestionList().get(i).getAuthor().getUserLastName() + "   ,Question: " + msgqrep.getQuestionList().get(i).getQuestion().toString());
			}
			listQuestions = new JList(questionsList);
			listQuestions.setVisibleRowCount(3);
			listQuestions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listQuestions.setSelectedIndex(0);
			listQuestions.addListSelectionListener(new ListSelectionListener() {
		        public void valueChanged(ListSelectionEvent evt) {
		        	int i = listQuestions.getSelectedIndex();
		        	
		        	txtQuestion.setText(qList.get(i).getQuestion());
		        	txtAnswer1.setText(qList.get(i).getAnswers()[0]);
		        	txtAnswer2.setText(qList.get(i).getAnswers()[1]);
		        	txtAnswer3.setText(qList.get(i).getAnswers()[2]);
		        	txtAnswer4.setText(qList.get(i).getAnswers()[3]);
		        	textSubject.setText(subjects.get(Integer.parseInt(qList.get(i).getQuestionSubject())-1).getSubjectName());
		        	textAuthor.setText(qList.get(i).getAuthor().getUserFirstName() + " " + qList.get(i).getAuthor().getUserLastName());        	
		        }
		      });
			questionScroller = new JScrollPane(listQuestions);
			questionScroller.setBounds(53, 147, 774, 225);
		} 
		return questionScroller;
	}
	
	private JTextPane gettxtQuestion()
	{
		if(txtQuestion == null)
		{
			txtQuestion = new JTextPane();
			txtQuestion.setFont(new Font("Tahoma", Font.PLAIN, 16));
			txtQuestion.setText("What is your name?");
			txtQuestion.setBounds(212, 410, 485, 30);
			
			txtAnswer1 = new JTextPane();
			txtAnswer1.setText("Michael");
			txtAnswer1.setBounds(212, 451, 485, 20);
			
			txtAnswer2 = new JTextPane();
			txtAnswer2.setText("Yaakov");
			txtAnswer2.setBounds(212, 482, 485, 20);
			
			txtAnswer3 = new JTextPane();
			txtAnswer3.setText("Amir");
			txtAnswer3.setBounds(212, 513, 485, 20);
			
			txtAnswer4 = new JTextPane();
			txtAnswer4.setText("Maya");
			txtAnswer4.setBounds(212, 544, 485, 20);
			
			lblQuestion = new JLabel("Question:");
			lblQuestion.setForeground(Color.BLUE);
			lblQuestion.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblQuestion.setBounds(107, 406, 91, 34);
		
			
			lblAnswer1 = new JLabel("Answer1:");
			lblAnswer1.setForeground(Color.BLUE);
			lblAnswer1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblAnswer1.setBounds(107, 451, 91, 20);
			
			lblAnswer2 = new JLabel("Answer2:");
			lblAnswer2.setForeground(Color.BLUE);
			lblAnswer2.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblAnswer2.setBounds(107, 482, 91, 20);
			
			lblAnswer3 = new JLabel("Answer3:");
			lblAnswer3.setForeground(Color.BLUE);
			lblAnswer3.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblAnswer3.setBounds(107, 513, 91, 20);
			
			lblAnswer4 = new JLabel("Answer4:");
			lblAnswer4.setForeground(Color.BLUE);
			lblAnswer4.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblAnswer4.setBounds(107, 544, 91, 20);
			
			lblAuthor = new JLabel("Author:");
			lblAuthor.setForeground(Color.BLUE);
			lblAuthor.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblAuthor.setBounds(721, 492, 91, 34);
			
			lblSubject = new JLabel("Subject:");
			lblSubject.setForeground(Color.BLUE);
			lblSubject.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
			lblSubject.setBounds(721, 410, 91, 34);
			
			textSubject = new JTextPane();
			textSubject.setFont(new Font("Tahoma", Font.PLAIN, 16));
			textSubject.setBounds(721, 440, 220, 30);
			
			textAuthor = new JTextPane();
			textAuthor.setFont(new Font("Tahoma", Font.PLAIN, 16));
			textAuthor.setBounds(721, 524, 220, 30);
		}
		return txtQuestion;
	}
}
